package net.caixiaomi.info.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.UserCouponAdapter;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.CouponsItem;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.R.id.button1;
import static android.R.id.list;
import static net.caixiaomi.info.util.EventWhat.EVENT_TAKE_ORDER_SELECT_COUPON;

/**
 * 赛事筛选
 */

public class CouponDialogFragment extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {

    @BindView(list)
    RecyclerView mListView;
    private UserCouponAdapter mAdapter;

    private List<CouponsItem> mData = new ArrayList<>();

    @OnClick(button1)
    void toConfirm() {
        CouponsItem item = null;
        Iterator<CouponsItem> iterator = mAdapter.getData().iterator();
        while (iterator.hasNext()) {
            CouponsItem next = iterator.next();
            if (next.isSelected()) {
                item = next;
                break;
            }
        }
        EventBus.getDefault().post(new CommonEvent(EVENT_TAKE_ORDER_SELECT_COUPON, JSON.toJSONString(item)));
        dismissAllowingStateLoss();
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_coupon;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String strData = getArguments().getString("data");
        List<CouponsItem> data = JSON.parseObject(strData, new TypeToken<List<CouponsItem>>() {
        }.getType());
        if (data != null && !data.isEmpty()) {
            mData.addAll(data);
        }
    }

    @Override
    protected void onViewCreated(@NonNull View view) {
        try {
            Window window = getDialog().getWindow();
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            wlp.gravity = Gravity.BOTTOM;
            window.setAttributes(wlp);
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setBackgroundDrawable(new ColorDrawable(0xff000000));
            getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);


            mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
            mAdapter = new UserCouponAdapter(R.layout.item_select_coupon);
            mAdapter.setOnItemClickListener(this);
            mAdapter.setNewData(mData);
            mListView.setAdapter(mAdapter);

            view.measure(0, 0);

            if (view.getMeasuredHeight() > AppHelper.getScreenHeight(getActivity()) / 3 * 2) {
                ViewGroup.LayoutParams layoutParams = mListView.getLayoutParams();
                layoutParams.height = AppHelper.getScreenHeight(getActivity()) / 3 * 2;
            }

            getDialog().setCanceledOnTouchOutside(true);
            getDialog().setCancelable(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            CouponsItem couponsItem = mAdapter.getData().get(position);
            if (couponsItem.isSelected()) {
                couponsItem.setSelected(!couponsItem.isSelected());
            } else {
                Iterator<CouponsItem> iterator = mAdapter.getData().iterator();
                while (iterator.hasNext()) {
                    CouponsItem next = iterator.next();
                    next.setSelected(false);
                }
                mAdapter.getData().get(position).setSelected(true);
            }

            mAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
